package cn.tradewin.advisor.persist.services.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;

import cn.tradewin.advisor.persist.mapper.JobMapper;
import cn.tradewin.advisor.persist.model.CodeLabel;
import cn.tradewin.advisor.persist.model.Job;
import cn.tradewin.advisor.persist.services.JobService;

public class JobServiceImpl implements JobService {
	
private final SqlSessionFactory sqlSessionFactory;
	
	private final Logger log;

	public JobServiceImpl(SqlSessionFactory sqlSessionFactory, Logger log) {
		this.log = log;
		this.sqlSessionFactory = sqlSessionFactory;
	}

	@Override
	public List<CodeLabel> getJobs() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			JobMapper mapper = session.getMapper(JobMapper.class);
			return mapper.selectCodeLabel();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	
	
	@Override
	public List<Job> getAllJobs() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			JobMapper mapper = session.getMapper(JobMapper.class);
			return mapper.selectAllJobs();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	
	@Override
	public Job findJob(String jobId) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			JobMapper mapper = session.getMapper(JobMapper.class);
			return mapper.selectJob(jobId);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int addJob(Job job) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			JobMapper mapper = session.getMapper(JobMapper.class);
			int count = mapper.insertJob(job);
			if (count < 0) {
				throw new RuntimeException("failure add Job [" + job.getId() + "]");
			}
			session.commit();
			return count;
		} catch (Exception e) {
			log.error(e.getMessage());
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int updJob(Job job) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			JobMapper mapper = session.getMapper(JobMapper.class);
			int count = mapper.updateJob(job);
			if (count < 0) {
				throw new RuntimeException("failure update Job [" + job.getId() + "]");
			}
			session.commit();
			return count;
		} catch (Exception e) {
			log.error(e.getMessage());
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int delJob(String jobId) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			JobMapper mapper = session.getMapper(JobMapper.class);
			int count = mapper.deleteJob(jobId);
			if (count < 0) {
				throw new RuntimeException("failure delete Job [" + jobId + "]");
			}
			session.commit();
			return count;
		} catch (Exception e) {
			log.error(e.getMessage());
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

}
